<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class Dataman_m extends MY_Model {

	public function __construct()
	{
		parent::__construct();
	}

	/*/////////////////////////////////// CONTENT SECTION ///////////////////////////////////////*/
	function get_total_content_records(){
		return count( $this->db->get('ff_rs_content')->result() );
	}
	
	function get_content( $num, $offset ){
		$result= $this->db->order_by('modified_time','desc')->get('`ff_rs_content`', $num, $offset)->result();
		
		foreach ($result as & $obj){
			$obj->state_name = $this->get_state_name($obj->region_id);
			$obj->city_name = $this->get_city_name($obj->city_id);
			$obj->created_user = $this->get_user_name($obj->created_by_id);
			$obj->modified_user = $this->get_user_name($obj->modified_by_id);
		}		
		return $result;
	}
	
	function search_content($keyword, $type){
		switch ($type){
			case 'id':
				$sql_query= "SELECT * FROM ff_rs_content WHERE id=".intval($keyword);
				break;
			default:	
			case 'name':
				$sql_query= "SELECT * FROM ff_rs_content WHERE name LIKE '%$keyword%'";
				break;
			case 'region':
				$sql_query= "SELECT A.* FROM ff_rs_content AS A INNER JOIN ff_regions AS B ON A.region_id= B.id
							 WHERE B.name LIKE '$keyword'";
				break;
			case 'city':
				$sql_query= "SELECT A.* FROM ff_rs_content AS A INNER JOIN ff_cities AS B ON A.city_id= B.id
							 WHERE B.city LIKE '$keyword'";
				break;
			case 'created_user':
				$sql_query= "SELECT A.* FROM ff_rs_content AS A INNER JOIN ff_users_fb AS B ON A.created_by_id= B.id
							 WHERE B.name LIKE '%$keyword%'";
				break;
			case 'modified_user':
				$sql_query= "SELECT A.* FROM ff_rs_content AS A INNER JOIN ff_users_fb AS B ON A.modified_by_id= B.id
							 WHERE B.name LIKE '%$keyword%'";
				break;
		}
		
		$result= $this->db->query($sql_query)->result();
			
		foreach ($result as & $obj){
			$obj->state_name = $this->get_state_name($obj->region_id);
			$obj->city_name = $this->get_city_name($obj->city_id);
			$obj->created_user = $this->get_user_name($obj->created_by_id);
			$obj->modified_user = $this->get_user_name($obj->modified_by_id);
		}		
		return $result;
	}
	/*/////////////////////////////////// AREA SECTION ///////////////////////////////////////*/
	function get_state_name($state_id){
		$r= $this->db->where('id',$state_id)->get('ff_regions')->result();
		if(!empty($r))
			return $r[0]->name; 
		return '';
	}
	
	function get_city_name($city_id){
		$r= $this->db->where('id',$city_id)->get('ff_cities')->result();
		if(!empty($r))
			return $r[0]->city; 
		return '';
	}
	
	function _regions( $selected_id = 0 ){
		$regions_obj_arr = $this->db->get('ff_regions')->result();

		$html = "<select name='region' id='region' onchange='javascript:selectCities(this.value);' class='select_postalcode' >";
		
		$select = ( $selected_id == 0 )? ' selected="selected"' : '';
		$html .= "<option value=\"0\" $select>".lang('ranking.select.regions').'</option>';
		
		foreach($regions_obj_arr as $obj){
			if($obj->id == $selected_id){
				$select = ' selected="selected"';
			}else{
				$select = '';
			}
			
			$html .= "<option value='$obj->id' $select>".html_entity_decode( ucfirst( $obj->name ) )."</option>";
		}
		$html .= "</select>";
		
		return $html;
	}
	
	function _cities( $region_id = -1 , $selected_id = 0 ){
		if( $region_id == -1 ){
			$cities_obj_arr = $this->db->get('ff_cities')->result();
		}else{
			$cities_obj_arr = $this->db->where('region_id',$region_id)->get('ff_cities')->result();
		}
		
		$html = "<select name='city' id='city' class='select_postalcode' >";
		$html .= '<option value="0">'.lang('ranking.select.cities').'</option>';
		
		// IF NOT CHOOSE REGIONS. CITIES IS DISABLE
		if($region_id != -1){
			foreach ($cities_obj_arr as $obj){
				if($obj->id == $selected_id){
					$select = ' selected="selected"';
				}else{
					$select = '';
				}
				
				$html .= "<option value='$obj->id' $select>".html_entity_decode( ucfirst( $obj->city ) )."</option>";
			}
		}
		$html .= "</select>";
		
		return $html;
	}
	
	function get_total_state_records(){
		return count($this->db->get('`ff_regions`')->result());
	}
	
	function get_state($num, $offset){
		return $this->db->get('`ff_regions`', $num, $offset)->result();
	}
	
	function get_total_cities_records(){
		return count($this->db->get('ff_cities')->result());
	}
	
	function get_cities($num, $offset){
		$offset = intval($offset);
		return $this->db->query("SELECT A.*, B.name AS state FROM ff_cities AS A INNER JOIN ff_regions AS B ON A.region_id=B.id ORDER BY A.id DESC LIMIT $offset,$num")->result();
	}
	/*///////////////////////////////////CONNECTION SECTION //////////////////////////////////*/
	function get_total_connection_records(){
		return count($this->db->get('rs_connection')->result());
	}
	
	function get_connection($num, $offset, $field, $order){
		$offset = intval($offset);
		return $this->db->query("SELECT * FROM rs_connection ORDER BY $field $order LIMIT $offset, $num")->result();
	}
	/*/////////////////////////////////// USER SECTION ///////////////////////////////////////*/
	function get_user_name($user_id= 0){
		$r= $this->db->where('id',$user_id)->get('ff_users_fb')->result();
		if(empty($r)) return '';
		return $r[0]->name; 
	}
	
	function get_total_user_records(){
		return count($this->db->get('ff_users_fb')->result());
	}
	
	function get_user($num, $offset){
		return $this->db->get('`ff_users_fb`', $num, $offset)->result();
	}
	
	function get_total_user_post_records($user_id){
	//	return count($this->db->query("SELECT * FROM rs_connection WHERE 
	//					posted_by REGEXP '({$user_id},)|{$user_id}$'")->result());
		return count($this->db->query("SELECT * FROM rs_post WHERE user_id=$user_id"));
	}
	
	function get_user_post($num, $offset, $user_id){
		$offset = intval($offset);
		 
		return $this->db->query("SELECT * FROM rs_post WHERE user_id=$user_id ORDER BY posted_time DESC LIMIT $offset,$num")->result();
	//	return $this->db->query("SELECT * FROM rs_connection WHERE 
	//					posted_by REGEXP '({$user_id},)|{$user_id}$' ORDER BY last_posted_time DESC 
	//					LIMIT $offset,$num")->result();	
	}
	
	function get_total_violate_records(){
		return count($this->db->get('ff_item_violate')->result());
	}
	
	function get_violate($num, $offset){
		$offset = intval($offset);
		return $this->db->query("SELECT ff_item_violate.*, ff_users_fb.name AS user_name, ff_rs_content.name AS item_name, ff_rs_content.id AS item_id
		   FROM ff_item_violate 
		   INNER JOIN ff_users_fb ON ff_item_violate.user_report_id = ff_users_fb.id 
		   INNER JOIN ff_rs_content ON ff_rs_content.id= ff_item_violate.item_id
		   LIMIT $offset, $num
		   ")->result();
	}
	
	
	
	
	
	
	
	
	
	
	
}
